﻿using System;
using IOP.Pulsar.Abstractions;
using Microsoft.Extensions.DependencyInjection;

namespace IOP.Pulsar.Server
{
    /// <summary>
    /// 服务器构建者
    /// </summary>
    public class ServerBuilder : IServerBuilder
    {

        /// <summary>
        /// 服务器服务
        /// </summary>
        public IServiceProvider ServerServices { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="services"></param>
        public ServerBuilder(IServiceProvider services)
        {
            ServerServices = services ?? throw new ArgumentNullException(nameof(services));
        }

        /// <summary>
        /// 构建管线
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="builder"></param>
        public void BuildPipeline<TContext>(Action<IProductLineBuilder<TContext>> builder) where TContext : class
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));
            var lineBuidler = ServerServices.GetRequiredService<IProductLineBuilder<TContext>>();
            builder(lineBuidler);
        }
    }
}
